package com.yushealth.prostate.fingerdanceforprostate;

import android.content.Context;


class FingerAction {

    private int duration = 30;  //  动作延时 3分钟
    private int factor;
    private Context context;

    private int cycle_count;
    private int cycle_count_cur;

    private int duration_cur;
    private int idx_prepare;

    private boolean finished;

    private int idx_start;
    private int drawable_start[];
    private int idx_back;
    private int drawable_back[];

    private int drawable_prepare[] = new int[]{
            R.drawable.num_3,
            R.drawable.num_2,
            R.drawable.num_1,
            R.drawable.s_0,
    };
    private int drawable_cur;

    // 阶段
    enum Stage {
        START, KEEP, BACK, PREPARE
    }

    ;
    private String stage_s[] = new String[]{"Bend", "Keep", "Restore", "Prepare"};

    private Stage stage;

    // 构造函数
    FingerAction(Context context, int cycle_count, int duration, int[] drawable_start, int[] drawable_back) {
        this.context = context;
        this.cycle_count = cycle_count;
        this.duration = duration;

        this.drawable_start = drawable_start;
        this.drawable_back = drawable_back;

        this.factor = 90 / cycle_count;

        reset();
    }

    // 复位
    void reset() {
        duration_cur = duration;
        cycle_count_cur = 0;

        finished = false;

        stage = Stage.PREPARE;
        idx_prepare = 0;

        idx_start = 0;
        idx_back = 0;
    }

    // 结束
    boolean isFinished() {
        return finished;
    }

    void run(int tick) {
        switch (stage) {
            case START:
                // 状态：扳下手指
                if (idx_start < drawable_start.length) {
                    drawable_cur = drawable_start[idx_start];
                    idx_start++;
                } else {
                    stage = Stage.KEEP;
                }
                break;

            case KEEP:
                // 状态：保持手指
                duration_cur -= tick;
                // 完成1个周期
                if (duration_cur <= 1) {
                    duration_cur = duration;
                    stage = Stage.BACK;
                }
                break;

            case BACK:
                // 状态： 恢复手指
                if (idx_back < drawable_back.length) {
                    drawable_cur = drawable_back[idx_back];
                    idx_back++;
                } else {
                    cycle_count_cur++;
                    // 是否完成所有周期
                    if (cycle_count_cur == cycle_count) {
                        finished = true;
                    }

                    stage = Stage.PREPARE;
                    idx_prepare = 0;
                }

                break;

            case PREPARE:

                if (idx_prepare < drawable_prepare.length) {
                    drawable_cur = drawable_prepare[idx_prepare];
                    idx_prepare++;
                } else {
                    stage = Stage.START;
                    idx_start = 0;
                    idx_back = 0;
                }
                break;

            // switch
        }

        //
    }


    // 返回drawable
    int get_drawable() {
        return drawable_cur;
    }

    // 已完成次数
    int getCycle_count() {
        return cycle_count_cur * factor;
    }

    // 动作持续时间
    int getDuration_cur() {
        return duration_cur;
    }

    // 剩余次数
    String getActionLeft() {
        return context.getString(R.string.m_left) + " " + Integer.toString(cycle_count - cycle_count_cur);
    }

    // 阶段状态
    String getStage() {
        return stage_s[stage.ordinal()];
    }

// end
}
